<?php

require_once(dirname(__FILE__) . '/appconf.php');

class Help extends Site {

    function setup() {
        $this->run_modes('show', 'about', 'contactus', 'policy', 'send', 'clear', 'tutorials');
        $this->start_mode('show');
        $this->selfurl = '/help.php';
        parent::setup();
        include("common.php");
    }

    function cgiapp_prerun($rm = '') {
        $this->Lookup = getModel('Model_Lookup');
        $this->Help = getModel('Model_Help');
        $this->Points = getModel('Model_Points');
        parent::cgiapp_prerun();
    }

    function action_show() {
        $kvcobj = getCache('help_show', array('expire' => KVC_EXPIRE));
        $tag_top = $kvcobj->get('tag_top');
        if (!$tag_top) {
            $tag_top = $this->Lookup->getLookup('help');
            $tag_top['result'][] = array('id' => '0', 'name' => '', 'cname' => 'iTrial分', 'topid' => '0');
            $kvcobj->set('tag_top', $tag_top);
        }
        //$tag_top2 = $this->Help->getHelp(2);
        //echo '<pre>'.var_export($tag_top,1).'</pre>';

        $tag_cc = $kvcobj->get('tag_cc');
        if (!$tag_cc) {
            $tag_cc = array();
            foreach ($tag_top['result'] as $k => $v) {
                $temp_arr = $this->Help->getHelp($v['id']);
                $tag_cc[$k] = $temp_arr['result'];
            }
            $kvcobj->set('tag_cc', $tag_cc);
        }
        $page = intval($this->get_q("page", 1, "int"));
        $tag = intval($this->get_q("tag", 1, "int"));


        $points = $kvcobj->get('points');
        if (!$points) {
            $points_add = $this->Points->get_points_action("", 1);
            $points_subtract = $this->Points->get_points_action("", 0);
            //$points = array_merge($points_add, $points_subtract);
            $points['add'] = $points_add;
            $points['aubtract'] = $points_subtract;
            $kvcobj->set('points', $points);
        }
        //echo '<pre>'.var_export($points,1).'</pre>';
        $this->tmpl_assign('tag_top', $tag_top);
        $this->tmpl_assign('tag_cc', $tag_cc);
        $this->tmpl_assign('points', $points);
        $this->tmpl_assign('tag', $tag);
        $this->tmpl_assign('page', $page);

        return $this->load_tmpl('help.php');
    }

    function action_about() {
        return $this->load_tmpl('about.php');
    }

    function action_send() {

        $reg_p = $this->get_q('reg_p');
        $captcha = $this->get_q('captcha');

        $capobj = getLibClass('Captcha');
        if ($capobj->check($captcha)) {
            $subject = "聯絡我們 - " . $reg_p['send_mail_address'];
            $content = "
" . $reg_p['subject'] . "
<br><br>
" . $reg_p['message'] . "
<br><br>
姓名：" . $reg_p['cname'] . "
<br><br>
email：" . $reg_p['email'] . "
<br><br>
phone：" . $reg_p['phone'] . "

";
            //$receiver = "zjlu@nmg.com.hk";
            $sender = "imore_enq@nmg.com.hk";
            $sender = $reg_p['email'];

            sendMail($receiver, $sender, $subject, $content);
            return showMsgCode("發送成功", '/');
        } else {
            return showMsgCode(ERR_CAPTCHA, optimize_url('help', 'contactus'));
        }
    }

    function action_contactus() {
        return $this->load_tmpl('contactus.php');
    }

    function action_policy() {
        $this->redirect("http://www.nmg.com.hk/privacy/");
        return $this->load_tmpl('policy.php');
    }

    function action_tutorials() {

        $step = $this->get_q('step', 1, 'int');
        $page = $this->get_q('page', 1, 'int');

        if (!$step) {
            // return showMsgCode("參數有誤", optimize_url('help', 'show'));
            return showMsgCode("參數有誤", "help.php");
        }

        $step_page = array(
            1 => array(5, 'trial_guide_reg_0', '註冊教學'),
            2 => array(8, 'trial_guide_test_0', '申請試用'),
            3 => array(5, 'trial_guide_report_0', '提交報告')
        );

        if ($page > $step_page[$step][0])
            $page = $step_page[$step][0];
        $img_src = "/images/" . $step_page[$step][1] . $page . ".jpg";

        $this->tmpl_assign('page', $page);
        $this->tmpl_assign('step_page', $step_page);
        $this->tmpl_assign('img_src', $img_src);
        $this->tmpl_assign('step', $step);

        return $this->load_tmpl('tutorials.php');
    }

    function action_clear() {
        $kvc = trim($this->get_q('kvc', ''));
        clear_kvcache($kvc);
    }

}

$webapp = new Help();
$webapp->run();
?>